.TH std::bitset::to_string 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::bitset::to_string \- std::bitset::to_string

.SH Synopsis
   template< class CharT, class Traits, class Allocator
   >

   std::basic_string<CharT, Traits, Allocator>                  \fI(until C++11)\fP
       to_string( CharT zero = CharT('0'),

                  CharT one = CharT('1') ) const;
   template<

       class CharT = char,
       class Traits = std::char_traits<CharT>,
       class Allocator = std::allocator<CharT>                  \fI(since C++11)\fP
   >                                                            (constexpr since C++23)
   std::basic_string<CharT, Traits, Allocator>
       to_string( CharT zero = CharT('0'),
                                                        \fB(1)\fP
                  CharT one = CharT('1') ) const;
   template< class CharT, class Traits >

   std::basic_string<CharT, Traits>                         \fB(2)\fP \fI(until C++11)\fP
       to_string( CharT zero = CharT('0'),

                  CharT one = CharT('1') ) const;
   template< class CharT >

   std::basic_string<CharT> to_string( CharT zero =
   CharT('0'),                                              \fB(3)\fP \fI(until C++11)\fP

                                       CharT one =
   CharT('1') ) const;
   std::string to_string( char zero = '0', char one =       \fB(4)\fP \fI(until C++11)\fP
   '1' ) const;

   Converts the contents of the bitset to a string. Uses zero to represent bits with
   value of false and one to represent bits with value of true.

   The resulting string contains N characters with the first character corresponds to
   the last (N-1^th) bit and the last character corresponding to the first bit.

   All template type arguments need to be provided because function
   templates cannot have default template arguments. Overloads (2-4) are
   provided to simplify the invocations of to_string:

   2) Uses the default allocator std::allocator.                          \fI(until C++11)\fP
   3) Uses the default character trait std::char_traits and the default
   allocator std::allocator.
   4) Uses the default character type char, the default character trait
   std::char_traits and the default allocator std::allocator.

.SH Parameters

   zero - character to use to represent false
   one  - character to use to represent true

.SH Return value

   1) The converted string.
   2) to_string<CharT, Traits, std::allocator<CharT>>(zero, one).
   3) to_string<CharT, std::char_traits<CharT>, std::allocator<CharT>>(zero, one).
   4) to_string<char, std::char_traits<char>, std::allocator<char>>(zero, one).

.SH Exceptions

   May throw std::bad_alloc from the std::basic_string constructor.

.SH Notes

   Since C++11, functions templates can have default template arguments. LWG issue 1113
   removed the helper overloads (2-4) and added the corresponding default template
   arguments in \fB(1)\fP.

.SH Example


// Run this code

 #include <bitset>
 #include <iostream>

 int main()
 {
     std::bitset<8> b{42};
     std::cout << b.to_string() << '\\n'
               << b.to_string('*') << '\\n'
               << b.to_string('O', 'X') << '\\n';
 }

.SH Output:

 00101010
 **1*1*1*
 OOXOXOXO

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

     DR    Applied to         Behavior as published              Correct behavior
                      zero and one bits were converted to
   LWG 396 C++98      characters 0                          added parameters to provide
                      and 1 (which do not correspond to '0' values for these characters
                      and '1')
   LWG 434 C++98      all template arguments needed to be   added overloads (2-4)
                      provided
                      overloads (2-4) did not have the
   LWG 853 C++98      default                               also added
                      arguments added by LWG issue 396

.SH See also

   to_ulong  returns an unsigned long integer representation of the data
             \fI(public member function)\fP
   to_ullong returns an unsigned long long integer representation of the data
   \fI(C++11)\fP   \fI(public member function)\fP
